/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package nine.men.cse361;

/**
 *
 * @author cso
 */
import java.awt.*;
import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import javax.swing.event.*;
import java.awt.geom.*;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.util.*;
import java.lang.*;
import java.lang.reflect.Array;
import java.net.URL;
import java.net.URLDecoder;
import javax.swing.border.Border;
import javax.swing.border.LineBorder;
import java.util.Random;

public class CirclePanel extends JFrame implements ActionListener, MouseListener {
    //Shape circle = new Ellipse2D.Float(100, 100, 100, 100);

    private BufferedImage image;
    static protected Label label;
    static protected Label infolabel;
    static protected Label infolabel1;
    //DPanel d;
    Image img;
    ArrayList piecesLocations = new ArrayList();
    ArrayList setPiece = new ArrayList();
    ArrayList positions = null; // possible positions for result given position x (see possible_positions())
    static char[][] board = new char[7][7];
    static JButton button;
    static JButton button1;     //use for starting new game in human-human mode
    static JButton button2;     //use for starting new game in autoplay mode
    static JButton button3;     //use for display rule of the game
    int PlayerOneCount = 0;
    int PlayerTwoCount = 0;
    int Turn = 1;
    int tempPrevLoc = -1;
    boolean mill = false;
    int PlayerOneBoard = 0;
    int PlayerTwoBoard = 0;
    boolean gameOver = false;
    boolean machineMode = false;
    boolean autoPlayMode = false;
    
//    public void init() {
//        getContentPane().setLayout(new BorderLayout());
//
//        d = new DPanel();
//        d.setBackground(Color.white);
//
//        getContentPane().add(d);
//
//        label = new Label("Drag rectangle around within the area");
//        getContentPane().add("South", label);
//        
//    }
    
    
    //new committed
    /*
     * Initialize the pieces location with x, y coordinate
     */
    public void setPiecesLoc() {   // need to replace with class
        //ArrayList point = new ArrayList();
        //point.add(1,106);
        // al.add(point); 
        //label.setText("Game started."+point);

        ArrayList point = new ArrayList();

        point.add(85);
        point.add(106);
        piecesLocations.add(point);
        point = new ArrayList();
        point.add(451);
        point.add(106);
        piecesLocations.add(point);
        point = new ArrayList();
        point.add(817);
        point.add(106);
        piecesLocations.add(point);
        point = new ArrayList();
        point.add(817);
        point.add(473);
        piecesLocations.add(point);
        point = new ArrayList();
        point.add(817);
        point.add(835);
        piecesLocations.add(point);
        point = new ArrayList();
        point.add(451);
        point.add(835);
        piecesLocations.add(point);
        point = new ArrayList();
        point.add(85);
        point.add(835);
        piecesLocations.add(point);
        point = new ArrayList();
        point.add(85);
        point.add(473);
        piecesLocations.add(point);

        point = new ArrayList();
        point.add(228);
        point.add(248);
        piecesLocations.add(point);
        point = new ArrayList();
        point.add(451);
        point.add(248);
        piecesLocations.add(point);
        point = new ArrayList();
        point.add(675);
        point.add(248);
        piecesLocations.add(point);
        point = new ArrayList();
        point.add(675);
        point.add(473);
        piecesLocations.add(point);
        point = new ArrayList();
        point.add(675);
        point.add(695);
        piecesLocations.add(point);
        point = new ArrayList();
        point.add(451);
        point.add(695);
        piecesLocations.add(point);
        point = new ArrayList();
        point.add(228);
        point.add(695);
        piecesLocations.add(point);
        point = new ArrayList();
        point.add(228);
        point.add(473);
        piecesLocations.add(point);

        point = new ArrayList();
        point.add(366);
        point.add(390);
        piecesLocations.add(point);
        point = new ArrayList();
        point.add(451);
        point.add(390);
        piecesLocations.add(point);
        point = new ArrayList();
        point.add(535);
        point.add(390);
        piecesLocations.add(point);
        point = new ArrayList();
        point.add(535);
        point.add(473);
        piecesLocations.add(point);
        point = new ArrayList();
        point.add(535);
        point.add(555);
        piecesLocations.add(point);
        point = new ArrayList();
        point.add(451);
        point.add(555);
        piecesLocations.add(point);
        point = new ArrayList();
        point.add(366);
        point.add(555);
        piecesLocations.add(point);
        point = new ArrayList();
        point.add(366);
        point.add(473);
        piecesLocations.add(point);


        for (int i = 0; i < piecesLocations.size(); i++) {
            setPiece.add(0);
        }
    }

    public CirclePanel() {
        setPiecesLoc();
        setSize(1100, 900);
        addMouseListener(this);
    }


    public static void initial_setup() {

        for (int i = 0; i < 7; i++) {
            for (int j = 0; j < 7; j++) {

                if (i == 0 && j == 0) {

                    board[i][j] = '0';

                } else if (i == 0 && j == 1) {

                    board[i][j] = 'x';

                } else if (i == 0 && j == 2) {

                    board[i][j] = 'x';

                } else if (i == 0 && j == 3) {

                    board[i][j] = '0';

                } else if (i == 0 && j == 4) {

                    board[i][j] = 'x';

                } else if (i == 0 && j == 5) {

                    board[i][j] = 'x';

                } else if (i == 0 && j == 6) {

                    board[i][j] = '0';

                } else if (i == 1 && j == 0) {

                    board[i][j] = 'x';

                } else if (i == 1 && j == 1) {

                    board[i][j] = '0';

                } else if (i == 1 && j == 2) {

                    board[i][j] = 'x';

                } else if (i == 1 && j == 3) {

                    board[i][j] = '0';

                } else if (i == 1 && j == 4) {

                    board[i][j] = 'x';

                } else if (i == 1 && j == 5) {

                    board[i][j] = '0';

                } else if (i == 1 && j == 6) {

                    board[i][j] = 'x';

                } else if (i == 2 && j == 0) {

                    board[i][j] = 'x';

                } else if (i == 2 && j == 1) {

                    board[i][j] = 'x';

                } else if (i == 2 && j == 2) {

                    board[i][j] = '0';

                } else if (i == 2 && j == 3) {

                    board[i][j] = '0';

                } else if (i == 2 && j == 4) {

                    board[i][j] = '0';

                } else if (i == 2 && j == 5) {

                    board[i][j] = 'x';

                } else if (i == 2 && j == 6) {

                    board[i][j] = 'x';

                } else if (i == 3 && j == 0) {

                    board[i][j] = '0';

                } else if (i == 3 && j == 1) {

                    board[i][j] = '0';

                } else if (i == 3 && j == 2) {

                    board[i][j] = '0';

                } else if (i == 3 && j == 3) {

                    board[i][j] = 'x';

                } else if (i == 3 && j == 4) {

                    board[i][j] = '0';

                } else if (i == 3 && j == 5) {

                    board[i][j] = '0';

                } else if (i == 3 && j == 6) {

                    board[i][j] = '0';

                } else if (i == 4 && j == 0) {

                    board[i][j] = 'x';

                } else if (i == 4 && j == 1) {

                    board[i][j] = 'x';

                } else if (i == 4 && j == 2) {

                    board[i][j] = '0';

                } else if (i == 4 && j == 3) {

                    board[i][j] = '0';

                } else if (i == 4 && j == 4) {

                    board[i][j] = '0';

                } else if (i == 4 && j == 5) {

                    board[i][j] = 'x';

                } else if (i == 4 && j == 6) {

                    board[i][j] = 'x';

                } else if (i == 5 && j == 0) {

                    board[i][j] = 'x';

                } else if (i == 5 && j == 1) {

                    board[i][j] = '0';

                } else if (i == 5 && j == 2) {

                    board[i][j] = 'x';

                } else if (i == 5 && j == 3) {

                    board[i][j] = '0';

                } else if (i == 5 && j == 4) {

                    board[i][j] = 'x';

                } else if (i == 5 && j == 5) {

                    board[i][j] = '0';

                } else if (i == 5 && j == 6) {

                    board[i][j] = 'x';

                } else if (i == 6 && j == 0) {

                    board[i][j] = '0';

                } else if (i == 6 && j == 1) {

                    board[i][j] = 'x';

                } else if (i == 6 && j == 2) {

                    board[i][j] = 'x';

                } else if (i == 6 && j == 3) {

                    board[i][j] = '0';

                } else if (i == 6 && j == 4) {

                    board[i][j] = 'x';

                } else if (i == 6 && j == 5) {

                    board[i][j] = 'x';

                } else if (i == 6 && j == 6) {

                    board[i][j] = '0';

                } else {
                    System.out.print("Unsual Event!");
                }
            }
        }
    }

    public void newGame() {
        piecesLocations = new ArrayList();
        setPiece = new ArrayList();
        board = new char[7][7];
        PlayerOneCount = 0;
        PlayerTwoCount = 0;
        tempPrevLoc = -1;
        mill = false;
        PlayerOneBoard = 0;
        PlayerTwoBoard = 0;
        gameOver = false;
        Turn = 1;
        initial_setup();
        setPiecesLoc();
        repaint();
    }
    
    public static void main(String[] args) {
                //TODO code application logic here
                initial_setup();
                CirclePanel frame = new CirclePanel();
                frame.addNotify();
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                Panel p = new Panel();
                button = new JButton("New Game");

                button.addActionListener(frame);

                p.add(button, BorderLayout.NORTH);
                
                button1 = new JButton("New Machine Game");
                button1.addActionListener(frame);
                p.add(button1, BorderLayout.NORTH);
                
                button2 = new JButton("New autoPlay Game");
                button2.addActionListener(frame);
                p.add(button2, BorderLayout.NORTH);
                
                button3 = new JButton("Rule");
                button3.addActionListener(frame);
                p.add(button3, BorderLayout.NORTH);
                
                frame.add(p, BorderLayout.NORTH);
                label = new Label("Welcome to the game!");
                infolabel = new Label("1st player: BLUE, 2nd player: RED");
                infolabel.setBackground(Color.GRAY);
                infolabel.setSize(5, 5);
                infolabel1 = new Label("RED");
                infolabel1.setBackground(Color.GRAY);
                infolabel1.setBounds(0, 21, 20, 10);


                //infolabel.setBorder(border);
                frame.add(label, BorderLayout.SOUTH);
                //frame.add(infolabel,BorderLayout.LINE_END);
                frame.setBounds(0, 0, 1200, 900);


                frame.setVisible(true);
            }

    public void actionPerformed(ActionEvent e) { // TODO: It has to work with multiple buttons
        
        String id = ((JButton)e.getSource()).getText();
        if(id.equals("New Machine Game")){
            
            machineMode = true;
            autoPlayMode = false;
            newGame();
           // JOptionPane.showMessageDialog(null,"New Machine Grame", "Information", JOptionPane.INFORMATION_MESSAGE);
            
        }else if (id.equals("New Game")){
            //JOptionPane.showMessageDialog(null,"New Game started", "Information", JOptionPane.INFORMATION_MESSAGE);
            label.setText("New Game started!"+e.getID() );
            machineMode = false;
            autoPlayMode = false;
            newGame();
        }else if (id.equals("Rule")){
            JLabel message = new JLabel("<html><body>Nine Men's Morris Rules of play:<br><br>"
                                        + "Each player has nine pieces, or men, which move among the board's twenty-four spots. <br>"
                                        + "The object of the game is to leave the opposing player with fewer than three pieces or, <br>"
                                        + "as in checkers, no legal moves.<br><br>"
                                        + " Placing the pieces<br>"
                                        + "The board at the beginning of the game, before any pieces have been placed. The game begins<br>"
                                        + "with an empty board. Players take turns placing their pieces on empty spots. If a player is<br>"
                                        + "able to form a straight row of three pieces along one of the board's lines (i.e. not diagonally),<br>"
                                        + "he has a mill and may remove one of his opponent's pieces from the board; removed pieces may<br>"
                                        + "not be placed again. Players must remove any other pieces first before removing a piece from a<br>"
                                        + "formed mill. Once all eighteen pieces have been used, players take turns moving.<br><br>"
                                        + " Moving the pieces<br>"
                                        + "To move, a player slides one of his pieces along a board line to an empty adjacent spot.<br>"
                                        + "If he cannot do so, he has lost the game. As in the placement stage, a player who aligns three<br>"
                                        + "of his pieces on a board<br> line has a mill and may remove one of his opponent's pieces, avoiding<br>"
                                        + "the removal of pieces in mills if at all possible. Any player reduced to two pieces is unable to<br>"
                                        + "remove any more opposing pieces and thus loses the game.<br><br>"
                                        + " Flying:<br>"
                                        + "In one common variation, once a player is reduced to three pieces, his pieces may flyto any empty<br>"
                                        + "spots, not only adjacent ones. Some sources of the rules say this is the way the game is played,<br>"
                                        + "some treat it as a variation, and some don't mention it at all.");
            //JOptionPane.showMessageDialog(null,"Each player has nine pieces, or men, which move among the board's twenty-four spots. The object of the game is to leave the opposing player with fewer than three pieces or, as in checkers, no legal moves./n Placing the pieces/n/n The board at the beginning of the game, before any pieces have been placed. The game begins with an empty board. Players take turns placing their pieces on empty spots. If a player is able to form a straight row of three pieces along one of the board's lines (i.e. not diagonally),<br> he has a mill and may remove one of his opponent's pieces from the board; removed pieces may not be placed again. Players must remove any other pieces first before removing a piece from a formed mill. Once all eighteen pieces have been used, players take turns moving./n/n Moving the pieces/nTo move, a player slides one of his pieces along a board line to an empty adjacent spot. If he cannot do so, he has lost the game. As in the placement stage, a player who aligns three of his pieces on a board line has a mill and may remove one of his opponent's pieces, avoiding the removal of pieces in mills if at all possible. Any player reduced to two pieces is unable to remove any more opposing pieces and thus loses the game./n/n Flying /n In one common variation, once a player is reduced to three pieces, his pieces may fly","Rule", JOptionPane.INFORMATION_MESSAGE);
JOptionPane.showMessageDialog(null, message);
        }
        
        
    }
        

    public void drawCircle(int x, int y) {
        Graphics g = this.getGraphics();

        URL myurl = this.getClass().getResource("9manmor.gif");
        String test;
        String decodedPath;
        try {
            decodedPath = URLDecoder.decode(myurl.toString(), "UTF-8");
        } catch (Exception ex) {
            decodedPath = "";
        }
        test = decodedPath.toString().replaceFirst("file:/", "/");
        img = Toolkit.getDefaultToolkit().getImage(test);
        //System.out.println("img:"+img);

        g.drawImage(img, 50, 70, 800, 800, null);  //draw the board, background image
        ArrayList s = new ArrayList();
        int x1;
        int y1;

        for (int i = 0; i < piecesLocations.size(); i++) {
            if (Integer.parseInt(setPiece.get(i).toString()) == 1) { //draw first player's pieces, to Blue
                s = (ArrayList) piecesLocations.get(i);
                x1 = Integer.parseInt(s.get(0).toString());
                y1 = Integer.parseInt(s.get(1).toString());

                g.drawOval(x1 - 25, y1 - 25, 50, 50);
                g.setColor(Color.BLUE);
                g.fillOval(x1 - 25, y1 - 25, 50, 50);
            }
            if (Integer.parseInt(setPiece.get(i).toString()) == 2) {  //draw the Second player's pieces, to RED
                s = (ArrayList) piecesLocations.get(i);
                x1 = Integer.parseInt(s.get(0).toString());
                y1 = Integer.parseInt(s.get(1).toString());

                g.drawOval(x1 - 25, y1 - 25, 50, 50);
                g.setColor(Color.RED);
                g.fillOval(x1 - 25, y1 - 25, 50, 50);
            }

        }

    }

    @Override
    public void paint(Graphics g) {

        drawCircle(x, y);
        //g.drawString("info:" + x + ", " + y, x + 40, y);
    }

    public void mouseMoved(MouseEvent e) {
    }
    int x, y;

    public ArrayList possible_positions(int x) {
        ArrayList positions_temp = new ArrayList();

        switch (x) {
            case 0:
                positions_temp.add(1);
                positions_temp.add(7);
                break;

            case 1:
                positions_temp.add(0);
                positions_temp.add(2);
                positions_temp.add(9);
                break;

            case 2:
                positions_temp.add(1);
                positions_temp.add(3);
                break;

            case 3:
                positions_temp.add(2);
                positions_temp.add(4);
                positions_temp.add(11);
                break;

            case 4:
                positions_temp.add(3);
                positions_temp.add(5);
                break;

            case 5:
                positions_temp.add(4);
                positions_temp.add(6);
                positions_temp.add(13);
                break;

            case 6:
                positions_temp.add(5);
                positions_temp.add(7);
                break;

            case 7:
                positions_temp.add(0);
                positions_temp.add(6);
                positions_temp.add(15);
                break;

            case 8:
                positions_temp.add(9);
                positions_temp.add(15);
                break;

            case 9:
                positions_temp.add(1);
                positions_temp.add(8);
                positions_temp.add(10);
                positions_temp.add(17);
                break;

            case 10:
                positions_temp.add(9);
                positions_temp.add(11);
                break;

            case 11:
                positions_temp.add(3);
                positions_temp.add(12);
                positions_temp.add(19);
                break;

            case 12:
                positions_temp.add(11);
                positions_temp.add(13);
                break;

            case 13:
                positions_temp.add(5);
                positions_temp.add(12);
                positions_temp.add(14);
                positions_temp.add(21);
                break;

            case 14:
                positions_temp.add(13);
                positions_temp.add(15);
                break;

            case 15:
                positions_temp.add(7);
                positions_temp.add(8);
                positions_temp.add(14);
                positions_temp.add(23);
                break;

            case 16:
                positions_temp.add(17);
                positions_temp.add(23);
                break;

            case 17:
                positions_temp.add(9);
                positions_temp.add(16);
                positions_temp.add(18);
                break;

            case 18:
                positions_temp.add(17);
                positions_temp.add(19);
                break;

            case 19:
                positions_temp.add(11);
                positions_temp.add(18);
                positions_temp.add(20);
                break;

            case 20:
                positions_temp.add(19);
                positions_temp.add(21);
                break;

            case 21:
                positions_temp.add(13);
                positions_temp.add(20);
                positions_temp.add(22);
                break;

            case 22:
                positions_temp.add(21);
                positions_temp.add(23);
                break;

            case 23:
                positions_temp.add(15);
                positions_temp.add(16);
                positions_temp.add(22);
                break;

            default:
                positions_temp = null;
        }
        return positions_temp;
    }

    
    /*Checks if there is result mill formed by result player's move*/
    public void checkMills(int x, int y) {
        
        if(x == 0 && y == 0){
            if ((board[0][3] == Character.forDigit(Turn, 10) && board[0][6] == Character.forDigit(Turn, 10)) || (board[3][0] == Character.forDigit(Turn, 10) && board[6][0] == Character.forDigit(Turn, 10)) ){
                mill = true;
            }
        }
        else if(x == 0 && y == 3){
            if ((board[0][0] == Character.forDigit(Turn, 10) && board[0][6] == Character.forDigit(Turn, 10)) || (board[1][3] == Character.forDigit(Turn, 10) && board[2][3] == Character.forDigit(Turn, 10)) ){
                mill = true;
            }
        }
        else if(x == 0 && y == 6){
            if ((board[0][0] == Character.forDigit(Turn, 10) && board[0][3] == Character.forDigit(Turn, 10)) || (board[3][6] == Character.forDigit(Turn, 10) && board[6][6] == Character.forDigit(Turn, 10)) ){
                mill = true;
            }
        }
        else if(x == 1 && y == 1){
            if ((board[1][3] == Character.forDigit(Turn, 10) && board[1][5] == Character.forDigit(Turn, 10)) || (board[3][1] == Character.forDigit(Turn, 10) && board[5][1] == Character.forDigit(Turn, 10)) ){
                mill = true;
            }
        }
        else if(x == 1 && y == 3){
            if ((board[1][1] == Character.forDigit(Turn, 10) && board[1][5] == Character.forDigit(Turn, 10)) || (board[0][3] == Character.forDigit(Turn, 10) && board[2][3] == Character.forDigit(Turn, 10)) ){
                mill = true;
            }
        }
        else if(x == 1 && y == 5){
            if ((board[1][1] == Character.forDigit(Turn, 10) && board[1][3] == Character.forDigit(Turn, 10)) || (board[3][5] == Character.forDigit(Turn, 10) && board[5][5] == Character.forDigit(Turn, 10)) ){
                mill = true;
            }
        }
        else if(x == 2 && y == 2){
            if ((board[2][3] == Character.forDigit(Turn, 10) && board[2][4] == Character.forDigit(Turn, 10)) || (board[3][2] == Character.forDigit(Turn, 10) && board[4][2] == Character.forDigit(Turn, 10)) ){
                mill = true;
            }
        }
        else if(x == 2 && y == 3){
            if ((board[2][2] == Character.forDigit(Turn, 10) && board[2][4] == Character.forDigit(Turn, 10)) || (board[0][3] == Character.forDigit(Turn, 10) && board[1][3] == Character.forDigit(Turn, 10)) ){
                mill = true;
            }
        }
        else if(x == 2 && y == 4){
            if ((board[2][2] == Character.forDigit(Turn, 10) && board[2][3] == Character.forDigit(Turn, 10)) || (board[3][4] == Character.forDigit(Turn, 10) && board[4][4] == Character.forDigit(Turn, 10)) ){
                mill = true;
            }
        }
        else if(x == 3 && y == 0){
            if ((board[0][0] == Character.forDigit(Turn, 10) && board[6][0] == Character.forDigit(Turn, 10)) || (board[3][1] == Character.forDigit(Turn, 10) && board[3][2] == Character.forDigit(Turn, 10)) ){
                mill = true;
            }
        }
        else if(x == 3 && y == 1){
            if ((board[1][1] == Character.forDigit(Turn, 10) && board[5][1] == Character.forDigit(Turn, 10)) || (board[3][0] == Character.forDigit(Turn, 10) && board[3][2] == Character.forDigit(Turn, 10)) ){
                mill = true;
            }
        }
        else if(x == 3 && y == 2){
            if ((board[2][2] == Character.forDigit(Turn, 10) && board[4][2] == Character.forDigit(Turn, 10)) || (board[3][0] == Character.forDigit(Turn, 10) && board[3][1] == Character.forDigit(Turn, 10)) ){
                mill = true;
            }
        }
        else if(x == 3 && y == 4){
            if ((board[2][4] == Character.forDigit(Turn, 10) && board[4][4] == Character.forDigit(Turn, 10)) || (board[3][5] == Character.forDigit(Turn, 10) && board[3][6] == Character.forDigit(Turn, 10)) ){
                mill = true;
                JOptionPane.showMessageDialog(null,"New bug?", "Information", JOptionPane.INFORMATION_MESSAGE);
            }
        }
        else if(x == 3 && y == 5){
            if ((board[1][5] == Character.forDigit(Turn, 10) && board[5][5] == Character.forDigit(Turn, 10)) || (board[3][4] == Character.forDigit(Turn, 10) && board[3][6] == Character.forDigit(Turn, 10)) ){
                mill = true;
            }
        }
        else if(x == 3 && y == 6){
            if ((board[0][6] == Character.forDigit(Turn, 10) && board[6][6] == Character.forDigit(Turn, 10)) || (board[3][4] == Character.forDigit(Turn, 10) && board[3][5] == Character.forDigit(Turn, 10)) ){
                mill = true;
            }
        }
        else if(x == 4 && y == 2){
            if ((board[2][2] == Character.forDigit(Turn, 10) && board[3][2] == Character.forDigit(Turn, 10)) || (board[4][3] == Character.forDigit(Turn, 10) && board[4][4] == Character.forDigit(Turn, 10)) ){
                mill = true;
            }
        }
        else if(x == 4 && y == 3){
            if ((board[5][3] == Character.forDigit(Turn, 10) && board[6][3] == Character.forDigit(Turn, 10)) || (board[4][2] == Character.forDigit(Turn, 10) && board[4][4] == Character.forDigit(Turn, 10)) ){
                mill = true;
            }
        }
        else if(x == 4 && y == 4){
            if ((board[2][4] == Character.forDigit(Turn, 10) && board[3][4] == Character.forDigit(Turn, 10)) || (board[4][2] == Character.forDigit(Turn, 10) && board[4][3] == Character.forDigit(Turn, 10)) ){
                mill = true;
            }
        }
        else if(x == 5 && y == 1){
            if ((board[1][1] == Character.forDigit(Turn, 10) && board[3][1] == Character.forDigit(Turn, 10)) || (board[5][3] == Character.forDigit(Turn, 10) && board[5][5] == Character.forDigit(Turn, 10)) ){
                mill = true;
            }
        }
        else if(x == 5 && y == 3){
            if ((board[4][3] == Character.forDigit(Turn, 10) && board[6][3] == Character.forDigit(Turn, 10)) || (board[5][1] == Character.forDigit(Turn, 10) && board[5][5] == Character.forDigit(Turn, 10)) ){
                mill = true;
            }
        }
        else if(x == 5 && y == 5){
            if ((board[1][5] == Character.forDigit(Turn, 10) && board[3][5] == Character.forDigit(Turn, 10)) || (board[5][1] == Character.forDigit(Turn, 10) && board[5][3] == Character.forDigit(Turn, 10)) ){
                mill = true;
            }
        }
        else if(x == 6 && y == 0){
            if ((board[0][0] == Character.forDigit(Turn, 10) && board[3][0] == Character.forDigit(Turn, 10)) || (board[6][3] == Character.forDigit(Turn, 10) && board[6][6] == Character.forDigit(Turn, 10)) ){
                mill = true;
            }
        }
        else if(x == 6 && y == 3){
            if ((board[4][3] == Character.forDigit(Turn, 10) && board[5][3] == Character.forDigit(Turn, 10)) || (board[6][0] == Character.forDigit(Turn, 10) && board[6][6] == Character.forDigit(Turn, 10)) ){
                mill = true;
            }
        }
        else if(x == 6 && y == 6){
            if ((board[0][6] == Character.forDigit(Turn, 10) && board[3][6] == Character.forDigit(Turn, 10)) || (board[6][0] == Character.forDigit(Turn, 10) && board[6][3] == Character.forDigit(Turn, 10)) ){
                mill = true;
            }
        }
        System.out.print("\n");
        for (int i =0; i< 7; i++){
            for (int j =0; j< 7; j++){
                System.out.print(board[i][j]+" ");
            }
            System.out.print("\n");
        }
        if(mill == true){
            label.setText("Mill formed by Player " + Turn + ". Please remove other player's piece");
        }
        
        if(Turn == 1){
            Turn = 2;
        }
        else if(Turn == 2){
            Turn = 1;
        }
        
    }
    
//    public void blockOpponentMill(int p) {
//        
//        if(x == 0 && y == 0){
//            if ((board[0][3] == Character.forDigit(Turn, 10) && board[0][6] == Character.forDigit(Turn, 10)) || (board[3][0] == Character.forDigit(Turn, 10) && board[6][0] == Character.forDigit(Turn, 10)) ){
//                mill = true;
//            }
//        }
//        
//    }
    
    /*
     * Function used to form result mill or prevent opposent player from form result mill
     * i.e: first, this function can used to return 3rd location to form mill, which chkThisNum == my player number, 2
     * second this function can used to return the 3rd location of opponent player that will be block, from forming result mill, when chkThisNum == opp. player number
     */
    
    public int[] findSpotToFormMill(int chkThisNum) {  // Only used this for function when placing piece,
       
        int[] result = {-1,-1};
        char cMyNum = Character.forDigit(chkThisNum, 10);
        char zero = Character.forDigit(0, 10);
        
        if       (board[0][0] == zero && ((board[0][3] == cMyNum && board[0][6] == cMyNum) || (board[3][0] == cMyNum && board[6][0] == cMyNum) )){
                result[0]=0;
                result[1]=0;
                return result;
        }else if (board[0][3] == zero && ((board[0][0] == cMyNum && board[0][6] == cMyNum) || (board[1][3] == cMyNum && board[2][3] == cMyNum) )){
                result[0]=0;
                result[1]=3;
                return result;
        }else if (board[0][6] == zero && ((board[0][0] == cMyNum && board[0][3] == cMyNum) || (board[3][6] == cMyNum && board[6][6] == cMyNum) )){
                result[0]=0;
                result[1]=6;
                return result;
        }else if (board[1][1] == zero && ((board[1][3] == cMyNum && board[1][5] == cMyNum) || (board[3][1] == cMyNum && board[5][1] == cMyNum) )){
                result[0]=1;
                result[1]=1;
                return result;
        }else if (board[1][3] == zero && ((board[1][1] == cMyNum && board[1][5] == cMyNum) || (board[0][3] == cMyNum && board[2][3] == cMyNum) )){
                result[0]=1;
                result[1]=1;
                return result;
        }else if (board[1][5] == zero && ((board[1][1] == cMyNum && board[1][3] == cMyNum) || (board[3][5] ==cMyNum && board[5][5] == cMyNum) )){
                result[0]=1;
                result[1]=5;
                return result;
        }else if (board[2][2] == zero && ((board[2][3] == cMyNum && board[2][4] == cMyNum) || (board[3][2] == cMyNum && board[4][2] == cMyNum) )){
                result[0]=2;
                result[1]=2;
                return result;
        }else if (board[2][3] == zero && ((board[2][2] == cMyNum && board[2][4] == cMyNum) || (board[1][3] == cMyNum && board[0][3] == cMyNum) )){
                result[0]=2;
                result[1]=3;
                return result;
        }else if (board[2][4] == zero && ((board[2][2] == cMyNum && board[2][3] == cMyNum) || (board[3][4] == cMyNum && board[4][4] == cMyNum) )){
                result[0]=2;
                result[1]=4;
                return result;
        }else if (board[3][0] == zero && ((board[3][1] == cMyNum && board[3][2] == cMyNum) || (board[0][0] == cMyNum && board[6][0] == cMyNum) )){
                result[0]=3;
                result[1]=0;
                return result;
        }else if (board[3][1] == zero && ((board[3][0] == cMyNum && board[3][2] == cMyNum) || (board[1][1] == cMyNum && board[5][1] == cMyNum) )){
                result[0]=3;
                result[1]=1;
                return result;
        }else if (board[3][2] == zero && ((board[3][0] == cMyNum && board[3][1] == cMyNum) || (board[2][2] == cMyNum && board[4][2] == cMyNum) )){
                result[0]=3;
                result[1]=2;
                return result;
        }else if (board[3][4] == zero && ((board[3][5] == cMyNum && board[3][6] == cMyNum) || (board[2][4] == cMyNum && board[4][4] == cMyNum) )){
                result[0]=3;
                result[1]=4;
                return result;
        }else if (board[3][5] == zero && ((board[3][4] == cMyNum && board[3][6] == cMyNum) || (board[1][5] == cMyNum && board[5][5] == cMyNum) )){
                result[0]=3;
                result[1]=5;
                return result;
        }else if (board[3][6] == zero && ((board[3][4] == cMyNum && board[3][5] == cMyNum) || (board[0][6] == cMyNum && board[6][6] == cMyNum) )){
                result[0]=3;
                result[1]=6;
                return result;
        }else if (board[4][2] == zero && ((board[4][3] == cMyNum && board[4][4] == cMyNum) || (board[2][2] == cMyNum && board[3][2] == cMyNum) )){
                result[0]=4;
                result[1]=2;
                return result;
        }else if (board[4][3] == zero && ((board[4][2] == cMyNum && board[4][4] == cMyNum) || (board[5][3] == cMyNum && board[6][3] == cMyNum) )){
                result[0]=4;
                result[1]=3;
                return result;
        }else if (board[4][4] == zero && ((board[4][2] == cMyNum && board[4][3] == cMyNum) || (board[2][4] == cMyNum && board[3][4] == cMyNum) )){
                result[0]=4;
                result[1]=4;
                return result;
        }else if (board[5][1] == zero && ((board[5][3] == cMyNum && board[5][5] == cMyNum) || (board[1][1] == cMyNum && board[3][1] == cMyNum) )){
                result[0]=5;
                result[1]=1;
                return result;
        }else if (board[5][3] == zero && ((board[5][1] == cMyNum && board[5][5] == cMyNum) || (board[4][3] == cMyNum && board[6][3] == cMyNum) )){
                result[0]=5;
                result[1]=3;
                return result;
        }else if (board[5][5] == zero && ((board[5][1] == cMyNum && board[5][3] == cMyNum) || (board[1][5] == cMyNum && board[3][5] == cMyNum) )){
                result[0]=5;
                result[1]=5;
                return result;
        }else if (board[6][0] == zero && ((board[6][3] == cMyNum && board[6][6] == cMyNum) || (board[0][0] == cMyNum && board[3][0] == cMyNum) )){
                result[0]=6;
                result[1]=0;
                return result;
        }else if (board[6][3] == zero && ((board[6][0] == cMyNum && board[6][6] == cMyNum) || (board[4][3] == cMyNum && board[5][3] == cMyNum) )){
                result[0]=6;
                result[1]=3;
                return result;
        }else if (board[6][6] == zero && ((board[6][0] == cMyNum && board[6][3] == cMyNum) || (board[0][6] == cMyNum && board[3][6] == cMyNum) )){
                result[0]=6;
                result[1]=6;
                return result;
        }
        
        return result;
    }
    
    /*
     * This function generate and pick a random point based chkThisNum (0 as empty spot, 1 as player1, or  2 as player 2)
     * If chkThisNum =0, a random empty spot is return, for the machine to place to.
     * If chkThisNum =1, a random player1's piece is selected, for move(if not succeeded(invalid move), machine can rerun this method, and pick another piece)
     * If chkThisNum =2, the same purpose...
     */
    
    public int[] selectSpotForValue(int chkThisNum) {  // Only used this for function when placing piece,
        char cNum = Character.forDigit(chkThisNum, 10);
        int[][] results;
        int[] result ={-1,-1};
        results = new int[30][2];
        
        int index = 0;
        for (int i = 0; i < 7; i++) {
            for (int j = 0; j < 7; j++) {

                if (board[i][j] == cNum && index < 30) {

                    results[index][0]= i;
                    results[index][1]= j;
                    index++;
                }
            }
        }
        Random randomGenerator = new Random();               //picked a ramdom point
        if(index >0){
            int randomInt = randomGenerator.nextInt(index);

            while ((randomInt >= index)){
                    randomInt = randomInt /2;
            }
            result[0] =results[randomInt][0];
            result[1]=results[randomInt][1];

        }
            return result;
        

    }

    public void mouseClicked(MouseEvent e) {
        if (gameOver == false){
            x = e.getX();
            y = e.getY();
            // String str = label.getText();
            //label.setText("x:" + x + " y:" + y);
            //setPoints();
            int x1;
            int y1;
            boolean add;
            ArrayList s;

            /* sets up the board */
            if (((PlayerOneCount < 9 && Turn == 1) || (PlayerTwoCount < 9 && Turn == 2)) && mill == false) {

                add = true;
                s = new ArrayList();
                for (int i = 0; i < piecesLocations.size(); i++) {

                    s = (ArrayList) piecesLocations.get(i);
                    x1 = Integer.parseInt(s.get(0).toString());
                    y1 = Integer.parseInt(s.get(1).toString());
                    if ((x1 - 30 < x && x < x1 + 30) && (y1 - 30 < y && y < y1 + 30)) {
                        
                        
                        int[] tempI = convertPiecetoNumXY(i);
                        if (board[tempI[0]][tempI[1]] == '0') {
                            //board[tempI[0]][tempI[1]] = '0';
                            board[tempI[0]][tempI[1]] = Character.forDigit(Turn, 10);
                            checkMills(tempI[0],tempI[1]);
                            //JOptionPane.showMessageDialog(null,"work2", "Information", JOptionPane.INFORMATION_MESSAGE);
                        }else {
                            add = false;
                        }
                        //Already rewrite, should be remove
//                        if (i == 0 && board[0][0] == '0') {
//                            board[0][0] = Character.forDigit(Turn, 10);
//                            checkMills(0,0);
//                        } else if (i == 1 && board[0][3] == '0') {
//                            board[0][3] = Character.forDigit(Turn, 10);
//                            checkMills(0,3);
//                        } else if (i == 2 && board[0][6] == '0') {
//                            board[0][6] = Character.forDigit(Turn, 10);
//                            checkMills(0,6);
//                        } else if (i == 3 && board[3][6] == '0') {
//                            board[3][6] = Character.forDigit(Turn, 10);
//                            checkMills(3,6);
//                        } else if (i == 4 && board[6][6] == '0') {
//                            board[6][6] = Character.forDigit(Turn, 10);
//                            checkMills(6,6);
//                        } else if (i == 5 && board[6][3] == '0') {
//                            board[6][3] = Character.forDigit(Turn, 10);
//                            checkMills(6,3);
//                        } else if (i == 6 && board[6][0] == '0') {
//                            board[6][0] = Character.forDigit(Turn, 10);
//                            checkMills(6,0);
//                        } else if (i == 7 && board[3][0] == '0') {
//                            board[3][0] = Character.forDigit(Turn, 10);
//                            checkMills(3,0);
//                        } else if (i == 8 && board[1][1] == '0') {
//                            board[1][1] = Character.forDigit(Turn, 10);
//                            checkMills(1,1);
//                        } else if (i == 9 && board[1][3] == '0') {
//                            board[1][3] = Character.forDigit(Turn, 10);
//                            checkMills(1,3);
//                        } else if (i == 10 && board[1][5] == '0') {
//                            board[1][5] = Character.forDigit(Turn, 10);
//                            checkMills(1,5);
//                        } else if (i == 11 && board[3][5] == '0') {
//                            board[3][5] = Character.forDigit(Turn, 10);
//                            checkMills(3,5);
//                        } else if (i == 12 && board[5][5] == '0') {
//                            board[5][5] = Character.forDigit(Turn, 10);
//                            checkMills(5,5);
//                        } else if (i == 13 && board[5][3] == '0') {
//                            board[5][3] = Character.forDigit(Turn, 10);
//                            checkMills(5,3);
//                        } else if (i == 14 && board[5][1] == '0') {
//                            board[5][1] = Character.forDigit(Turn, 10);
//                            checkMills(5,1);
//                        } else if (i == 15 && board[3][1] == '0') {
//                            board[3][1] = Character.forDigit(Turn, 10);
//                            checkMills(3,1);
//                        } else if (i == 16 && board[2][2] == '0') {
//                            board[2][2] = Character.forDigit(Turn, 10);
//                            checkMills(2,2);
//                        } else if (i == 17 && board[2][3] == '0') {
//                            board[2][3] = Character.forDigit(Turn, 10);
//                            checkMills(2,3);
//                        } else if (i == 18 && board[2][4] == '0') {
//                            board[2][4] = Character.forDigit(Turn, 10);
//                            checkMills(2,4);
//                        } else if (i == 19 && board[3][4] == '0') {
//                            board[3][4] = Character.forDigit(Turn, 10);
//                            checkMills(3,4);
//                        } else if (i == 20 && board[4][4] == '0') {
//                            board[4][4] = Character.forDigit(Turn, 10);
//                            checkMills(4,4);
//                        } else if (i == 21 && board[4][3] == '0') {
//                            board[4][3] = Character.forDigit(Turn, 10);
//                            checkMills(4,3);
//                        } else if (i == 22 && board[4][2] == '0') {
//                            board[4][2] = Character.forDigit(Turn, 10);
//                            checkMills(4,2);
//                        } else if (i == 23 && board[3][2] == '0') {
//                            board[3][2] = Character.forDigit(Turn, 10);
//                            checkMills(3,2);
//                        } else {
//                            add = false;
//                        }

                        if (Turn == 2 && add) {
                            setPiece.set(i, 1);
                            PlayerOneCount++;
                            PlayerOneBoard++;
                            if (machineMode == true){                  //use for Human-Machine Mode, need to change to true
                                
                                int[] millSpot = findSpotToFormMill(2);
                                if (millSpot[0] != -1 && millSpot[1] != -1){  //if it returns  NOT {-1,-1}, potential threat found, that opp. player will form mill in the next move. so machine need to place on this spot to block
                                    label.setText("potential mill found");
                                    if (board[millSpot[0]][millSpot[1]] == '0'){
                                        int Num = convertXYtoPieceNum(millSpot[0], millSpot[1]);
                                        setPiece.set(Num, Turn);
                                        PlayerTwoCount++;
                                        PlayerTwoBoard++;
                                        board[millSpot[0]][millSpot[1]] = Character.forDigit(Turn, 10);
                                        checkMills(millSpot[0],millSpot[1]);
                                        
                                        
                                    }else{
                                        label.setText("Invalid placeing on ("+millSpot[0]+","+millSpot[1]);
                                    }
                                }else{
                                    int[] defendSpot = findSpotToFormMill(1);
                                    if (defendSpot[0] != -1 && defendSpot[1] != -1){  //try to block opponent player from form mill
                                        label.setText("Threat found");
                                        if (board[defendSpot[0]][defendSpot[1]] == '0'){
                                            int Num = convertXYtoPieceNum(defendSpot[0], defendSpot[1]);
                                            setPiece.set(Num, Turn);
                                            PlayerTwoCount++;
                                            PlayerTwoBoard++;
                                            board[defendSpot[0]][defendSpot[1]] = Character.forDigit(Turn, 10);
                                            checkMills(defendSpot[0],defendSpot[1]);
                                            
                                            
                                        }else{
                                            label.setText("Invalid placeing on ("+defendSpot[0]+","+defendSpot[1]);
                                        }
                                    }else{
                                        //label.setText("Threat Not found. move to next case");
                                        //random piece?

                                                int[] randomP = selectSpotForValue(0);
                                                if (board[randomP[0]][randomP[1]] == '0'){
                                                    int Num = convertXYtoPieceNum(randomP[0], randomP[1]);
                                                    setPiece.set(Num, Turn);
                                                    PlayerTwoCount++;
                                                    PlayerTwoBoard++;
                                                    board[randomP[0]][randomP[1]] = Character.forDigit(Turn, 10);
                                                    checkMills(randomP[0],randomP[1]);


                                                }else{
                                                    label.setText("Invalid placeing on ("+randomP[0]+","+randomP[1]);

                                                }
                                        
                                            
                                        
                                        
                                    }
                                }
                                
                            }else{                                      //human-human mode
                               if(mill == false){
                                    label.setText("First Player placed piece " + PlayerOneCount + ". Now second player's turn.");
                                } 
                            }
                            
                            
                        } else if (Turn == 1 && add) {
                            setPiece.set(i, 2);
                            PlayerTwoCount++;
                            PlayerTwoBoard++;
                            
                            
                            if(mill == false){
                                label.setText("Second Player placed piece " + PlayerTwoCount + ". Now First player's turn.");
                            }
                        }

                    }

                }

                repaint();
                    
            } 
            /*allows for player one's moves*/
            else if (PlayerOneCount >= 9 && Turn == 1 && mill == false) {  //after users placed the 9th piece
                add = true;
                s = new ArrayList();
                for (int i = 0; i < piecesLocations.size(); i++) {

                    s = (ArrayList) piecesLocations.get(i);
                    x1 = Integer.parseInt(s.get(0).toString());
                    y1 = Integer.parseInt(s.get(1).toString());
                    if ((x1 - 20 < x && x < x1 + 20) && (y1 - 20 < y && y < y1 + 20)) {

                        if (setPiece.get(i).toString().equalsIgnoreCase("1")) {
                            tempPrevLoc = i;
                            label.setText("First Player picked the piece, now select the distination location.");
                            
                            int[] tempI = convertPiecetoNumXY(i);
                            if (board[tempI[0]][tempI[1]] == '1') {
                                board[tempI[0]][tempI[1]] = '0';
                                ////board[tempI[0]][tempI[1]] = Character.forDigit(Turn, 10);
                                ///checkMills(tempI[0],tempI[1]);
                                //JOptionPane.showMessageDialog(null,"work2", "Information", JOptionPane.INFORMATION_MESSAGE);
                            }
                            //Already rewrite, should be remove
//                            if (i == 0 && board[0][0] == '1') {
//                                board[0][0] = '0';
//                            } else if (i == 1 && board[0][3] == '1') {
//                                board[0][3]= '0';
//                            } else if (i == 2 && board[0][6] == '1') {
//                                board[0][6]= '0';
//                            } else if (i == 3 && board[3][6] == '1') {
//                                board[3][6]= '0';
//                            } else if (i == 4 && board[6][6] == '1') {
//                                board[6][6]= '0';
//                            } else if (i == 5 && board[6][3] == '1') {
//                                board[6][3]= '0';
//                            } else if (i == 6 && board[6][0] == '1') {
//                                board[6][0]= '0';
//                            } else if (i == 7 && board[3][0] == '1') {
//                                board[3][0]= '0';
//                            } else if (i == 8 && board[1][1] == '1') {
//                                board[1][1]= '0';
//                            } else if (i == 9 && board[1][3] == '1') {
//                                board[1][3]= '0';
//                            } else if (i == 10 && board[1][5] == '1') {
//                                board[1][5]= '0';
//                            } else if (i == 11 && board[3][5] == '1') {
//                                board[3][5]= '0';
//                            } else if (i == 12 && board[5][5] == '1') {
//                                board[5][5]= '0';
//                            } else if (i == 13 && board[5][3] == '1') {
//                                board[5][3]= '0';
//                            } else if (i == 14 && board[5][1] == '1') {
//                                board[5][1]= '0';
//                            } else if (i == 15 && board[3][1] == '1') {
//                                board[3][1]= '0';
//                            } else if (i == 16 && board[2][2] == '1') {
//                                board[2][2]= '0';
//                            } else if (i == 17 && board[2][3] == '1') {
//                                board[2][3]= '0';
//                            } else if (i == 18 && board[2][4] == '1') {
//                                board[2][4]= '0';
//                            } else if (i == 19 && board[3][4] == '1') {
//                                board[3][4]= '0';
//                            } else if (i == 20 && board[4][4] == '1') {
//                                board[4][4]= '0';
//                            } else if (i == 21 && board[4][3] == '1') {
//                                board[4][3]= '0';
//                            } else if (i == 22 && board[4][2] == '1') {
//                                board[4][2]= '0';
//                            } else if (i == 23 && board[3][2] == '1') {
//                                board[3][2]= '0';
//                            } 
                        } else if (setPiece.get(i).toString().equalsIgnoreCase("2")) {
                            label.setText("Wrong Peg, select one of your pegs Player " + Turn);
                        } else {
 
                            if (tempPrevLoc != -1) {
                                positions = possible_positions(tempPrevLoc);

                                if (positions.contains(i)) {
                                    setPiece.set(i, Turn);
                                    setPiece.set(tempPrevLoc, 0);
                                    tempPrevLoc = -1;
                                    
                                    int[] tempI = convertPiecetoNumXY(i);
                                    if (board[tempI[0]][tempI[1]] == '0') {
                                        //board[tempI[0]][tempI[1]] = '0';
                                        board[tempI[0]][tempI[1]] = Character.forDigit(Turn, 10);
                                        checkMills(tempI[0],tempI[1]);
                                        //JOptionPane.showMessageDialog(null,"work2", "Information", JOptionPane.INFORMATION_MESSAGE);
                                    }
                                    //Already rewrite, should be remove
//                                    if (i == 0 && board[0][0] == '0') {
//                                        board[0][0] = Character.forDigit(Turn, 10);
//                                        checkMills(0,0);
//                                    } else if (i == 1 && board[0][3] == '0') {
//                                        board[0][3] = Character.forDigit(Turn, 10);
//                                        checkMills(0,3);
//                                    } else if (i == 2 && board[0][6] == '0') {
//                                        board[0][6] = Character.forDigit(Turn, 10);
//                                        checkMills(0,6);
//                                    } else if (i == 3 && board[3][6] == '0') {
//                                        board[3][6] = Character.forDigit(Turn, 10);
//                                        checkMills(3,6);
//                                    } else if (i == 4 && board[6][6] == '0') {
//                                        board[6][6] = Character.forDigit(Turn, 10);
//                                        checkMills(6,6);
//                                    } else if (i == 5 && board[6][3] == '0') {
//                                        board[6][3] = Character.forDigit(Turn, 10);
//                                        checkMills(6,3);
//                                    } else if (i == 6 && board[6][0] == '0') {
//                                        board[6][0] = Character.forDigit(Turn, 10);
//                                        checkMills(6,0);
//                                    } else if (i == 7 && board[3][0] == '0') {
//                                        board[3][0] = Character.forDigit(Turn, 10);
//                                        checkMills(3,0);
//                                    } else if (i == 8 && board[1][1] == '0') {
//                                        board[1][1] = Character.forDigit(Turn, 10);
//                                        checkMills(1,1);
//                                    } else if (i == 9 && board[1][3] == '0') {
//                                        board[1][3] = Character.forDigit(Turn, 10);
//                                        checkMills(1,3);
//                                    } else if (i == 10 && board[1][5] == '0') {
//                                        board[1][5] = Character.forDigit(Turn, 10);
//                                        checkMills(1,5);
//                                    } else if (i == 11 && board[3][5] == '0') {
//                                        board[3][5] = Character.forDigit(Turn, 10);
//                                        checkMills(3,5);
//                                    } else if (i == 12 && board[5][5] == '0') {
//                                        board[5][5] = Character.forDigit(Turn, 10);
//                                        checkMills(5,5);
//                                    } else if (i == 13 && board[5][3] == '0') {
//                                        board[5][3] = Character.forDigit(Turn, 10);
//                                        checkMills(5,3);
//                                    } else if (i == 14 && board[5][1] == '0') {
//                                        board[5][1] = Character.forDigit(Turn, 10);
//                                        checkMills(5,1);
//                                    } else if (i == 15 && board[3][1] == '0') {
//                                        board[3][1] = Character.forDigit(Turn, 10);
//                                        checkMills(3,1);
//                                    } else if (i == 16 && board[2][2] == '0') {
//                                        board[2][2] = Character.forDigit(Turn, 10);
//                                        checkMills(2,2);
//                                    } else if (i == 17 && board[2][3] == '0') {
//                                        board[2][3] = Character.forDigit(Turn, 10);
//                                        checkMills(2,3);
//                                    } else if (i == 18 && board[2][4] == '0') {
//                                        board[2][4] = Character.forDigit(Turn, 10);
//                                        checkMills(2,4);
//                                    } else if (i == 19 && board[3][4] == '0') {
//                                        board[3][4] = Character.forDigit(Turn, 10);
//                                        checkMills(3,4);
//                                    } else if (i == 20 && board[4][4] == '0') {
//                                        board[4][4] = Character.forDigit(Turn, 10);
//                                        checkMills(4,4);
//                                    } else if (i == 21 && board[4][3] == '0') {
//                                        board[4][3] = Character.forDigit(Turn, 10);
//                                        checkMills(4,3);
//                                    } else if (i == 22 && board[4][2] == '0') {
//                                        board[4][2] = Character.forDigit(Turn, 10);
//                                        checkMills(4,2);
//                                    } else if (i == 23 && board[3][2] == '0') {
//                                        board[3][2] = Character.forDigit(Turn, 10);
//                                        checkMills(3,2);
//                                    }
                                    
                                    if (mill == false){
                                    
                                        label.setText("First Player moved the piece. Now second player's turn.");
                                    }
                                } else {
                                    label.setText("Illegal move Player " + Turn + "! Select a free spot!");
                                }
                            }
                        }
                    }
                }

                repaint();

            } 
            /*allows player two's moves*/
            else if (PlayerTwoCount >= 9 && Turn == 2 && mill == false) {
                add = true;
                s = new ArrayList();
                for (int i = 0; i < piecesLocations.size(); i++) {

                    s = (ArrayList) piecesLocations.get(i);
                    x1 = Integer.parseInt(s.get(0).toString());
                    y1 = Integer.parseInt(s.get(1).toString());
                    if ((x1 - 20 < x && x < x1 + 20) && (y1 - 20 < y && y < y1 + 20)) {

                        if (setPiece.get(i).toString().equalsIgnoreCase("2")) {
                            tempPrevLoc = i;
                            label.setText("2nd Player picked the piece, now select the distination location.");
                            
                            int[] tempI = convertPiecetoNumXY(i);
                            if (board[tempI[0]][tempI[1]] == '2') {
                                board[tempI[0]][tempI[1]] = '0';
                                //JOptionPane.showMessageDialog(null,"work2", "Information", JOptionPane.INFORMATION_MESSAGE);
                            }
//                            if (i == 0 && board[0][0] == '2') {
//                                board[0][0] = '0';
//                            } else if (i == 1 && board[0][3] == '2') {
//                                board[0][3]= '0';
//                            } else if (i == 2 && board[0][6] == '2') {
//                                board[0][6]= '0';
//                            } else if (i == 3 && board[3][6] == '2') {
//                                board[3][6]= '0';
//                            } else if (i == 4 && board[6][6] == '2') {
//                                board[6][6]= '0';
//                            } else if (i == 5 && board[6][3] == '2') {
//                                board[6][3]= '0';
//                            } else if (i == 6 && board[6][0] == '2') {
//                                board[6][0]= '0';
//                            } else if (i == 7 && board[3][0] == '2') {
//                                board[3][0]= '0';
//                            } else if (i == 8 && board[1][1] == '2') {
//                                board[1][1]= '0';
//                            } else if (i == 9 && board[1][3] == '2') {
//                                board[1][3]= '0';
//                            } else if (i == 10 && board[1][5] == '2') {
//                                board[1][5]= '0';
//                            } else if (i == 11 && board[3][5] == '2') {
//                                board[3][5]= '0';
//                            } else if (i == 12 && board[5][5] == '2') {
//                                board[5][5]= '0';
//                            } else if (i == 13 && board[5][3] == '2') {
//                                board[5][3]= '0';
//                            } else if (i == 14 && board[5][1] == '2') {
//                                board[5][1]= '0';
//                            } else if (i == 15 && board[3][1] == '2') {
//                                board[3][1]= '0';
//                            } else if (i == 16 && board[2][2] == '2') {
//                                board[2][2]= '0';
//                            } else if (i == 17 && board[2][3] == '2') {
//                                board[2][3]= '0';
//                            } else if (i == 18 && board[2][4] == '2') {
//                                board[2][4]= '0';
//                            } else if (i == 19 && board[3][4] == '2') {
//                                board[3][4]= '0';
//                            } else if (i == 20 && board[4][4] == '2') {
//                                board[4][4]= '0';
//                            } else if (i == 21 && board[4][3] == '2') {
//                                board[4][3]= '0';
//                            } else if (i == 22 && board[4][2] == '2') {
//                                board[4][2]= '0';
//                            } else if (i == 23 && board[3][2] == '2') {
//                                board[3][2]= '0';
//                            } 
                        } else if (setPiece.get(i).toString().equalsIgnoreCase("1")) {
                            label.setText("Wrong Peg, select one of your pegs Player " + Turn);
                        } else {
                            if (tempPrevLoc != -1) {
                                positions = possible_positions(tempPrevLoc);
                                
                                if (positions.contains(i)) {
                                    setPiece.set(i, Turn);
                                    setPiece.set(tempPrevLoc, 0);
                                    tempPrevLoc = -1;
                                    
                                    int[] tempI = convertPiecetoNumXY(i);
                                    if (board[tempI[0]][tempI[1]] == '0') {
                                        board[tempI[0]][tempI[1]] = Character.forDigit(Turn, 10);
                                        //board[0][0] = Character.forDigit(Turn, 10);
                                        checkMills(tempI[0],tempI[1]);
                                        //JOptionPane.showMessageDialog(null,"work2", "Information", JOptionPane.INFORMATION_MESSAGE);
                                    }
                                    
                                    //IS REPLACED BY ABOUT METHOD
//                                    if (i == 0 && board[0][0] == '0') {
//                                        board[0][0] = Character.forDigit(Turn, 10);
//                                        checkMills(0,0);
//                                    } else if (i == 1 && board[0][3] == '0') {
//                                        board[0][3] = Character.forDigit(Turn, 10);
//                                        checkMills(0,3);
//                                    } else if (i == 2 && board[0][6] == '0') {
//                                        board[0][6] = Character.forDigit(Turn, 10);
//                                        checkMills(0,6);
//                                    } else if (i == 3 && board[3][6] == '0') {
//                                        board[3][6] = Character.forDigit(Turn, 10);
//                                        checkMills(3,6);
//                                    } else if (i == 4 && board[6][6] == '0') {
//                                        board[6][6] = Character.forDigit(Turn, 10);
//                                        checkMills(6,6);
//                                    } else if (i == 5 && board[6][3] == '0') {
//                                        board[6][3] = Character.forDigit(Turn, 10);
//                                        checkMills(6,3);
//                                    } else if (i == 6 && board[6][0] == '0') {
//                                        board[6][0] = Character.forDigit(Turn, 10);
//                                        checkMills(6,0);
//                                    } else if (i == 7 && board[3][0] == '0') {
//                                        board[3][0] = Character.forDigit(Turn, 10);
//                                        checkMills(3,0);
//                                    } else if (i == 8 && board[1][1] == '0') {
//                                        board[1][1] = Character.forDigit(Turn, 10);
//                                        checkMills(1,1);
//                                    } else if (i == 9 && board[1][3] == '0') {
//                                        board[1][3] = Character.forDigit(Turn, 10);
//                                        checkMills(1,3);
//                                    } else if (i == 10 && board[1][5] == '0') {
//                                        board[1][5] = Character.forDigit(Turn, 10);
//                                        checkMills(1,5);
//                                    } else if (i == 11 && board[3][5] == '0') {
//                                        board[3][5] = Character.forDigit(Turn, 10);
//                                        checkMills(3,5);
//                                    } else if (i == 12 && board[5][5] == '0') {
//                                        board[5][5] = Character.forDigit(Turn, 10);
//                                        checkMills(5,5);
//                                    } else if (i == 13 && board[5][3] == '0') {
//                                        board[5][3] = Character.forDigit(Turn, 10);
//                                        checkMills(5,3);
//                                    } else if (i == 14 && board[5][1] == '0') {
//                                        board[5][1] = Character.forDigit(Turn, 10);
//                                        checkMills(5,1);
//                                    } else if (i == 15 && board[3][1] == '0') {
//                                        board[3][1] = Character.forDigit(Turn, 10);
//                                        checkMills(3,1);
//                                    } else if (i == 16 && board[2][2] == '0') {
//                                        board[2][2] = Character.forDigit(Turn, 10);
//                                        checkMills(2,2);
//                                    } else if (i == 17 && board[2][3] == '0') {
//                                        board[2][3] = Character.forDigit(Turn, 10);
//                                        checkMills(2,3);
//                                    } else if (i == 18 && board[2][4] == '0') {
//                                        board[2][4] = Character.forDigit(Turn, 10);
//                                        checkMills(2,4);
//                                    } else if (i == 19 && board[3][4] == '0') {
//                                        board[3][4] = Character.forDigit(Turn, 10);
//                                        checkMills(3,4);
//                                    } else if (i == 20 && board[4][4] == '0') {
//                                        board[4][4] = Character.forDigit(Turn, 10);
//                                        checkMills(4,4);
//                                    } else if (i == 21 && board[4][3] == '0') {
//                                        board[4][3] = Character.forDigit(Turn, 10);
//                                        checkMills(4,3);
//                                    } else if (i == 22 && board[4][2] == '0') {
//                                        board[4][2] = Character.forDigit(Turn, 10);
//                                        checkMills(4,2);
//                                    } else if (i == 23 && board[3][2] == '0') {
//                                        board[3][2] = Character.forDigit(Turn, 10);
//                                        checkMills(3,2);
//                                    }
                                    if(mill == false){
                                        label.setText("Second Player moved the piece. Now it's 1st player's turn.");
                                    }
                                }
                                else {
                                    label.setText("Illegal move Player " + Turn + "! Select a free spot!");
                                }
                            }

                        }

                    }
                }

                repaint();

            }
            /*removes result piece if result mill has been formed */
            else if(mill == true){
                add = true;
                s = new ArrayList();
                for (int i = 0; i < piecesLocations.size(); i++) {

                    s = (ArrayList) piecesLocations.get(i);
                    x1 = Integer.parseInt(s.get(0).toString());
                    y1 = Integer.parseInt(s.get(1).toString());
                    if ((x1 - 20 < x && x < x1 + 20) && (y1 - 20 < y && y < y1 + 20)) {

                        if (Turn == 2 && setPiece.get(i).toString().equalsIgnoreCase("2")) {
                            setPiece.set(i, 0);
                            mill = false;
                            PlayerTwoBoard--;
                            label.setText("Player " + Turn + "'s piece removed");
                            
                            int[] tempI = convertPiecetoNumXY(i);
                            if (board[tempI[0]][tempI[1]] == '2') {
                                board[tempI[0]][tempI[1]] = '0';
                                //JOptionPane.showMessageDialog(null,"work2", "Information", JOptionPane.INFORMATION_MESSAGE);
                            }
                            //IS REPLACED BY ABOUT METHOD
//                            if (i == 0 && board[0][0] == '2') {
//                                board[0][0] = '0';
//                            } else if (i == 1 && board[0][3] == '2') {
//                                board[0][3]= '0';
//                            } else if (i == 2 && board[0][6] == '2') {
//                                board[0][6]= '0';
//                            } else if (i == 3 && board[3][6] == '2') {
//                                board[3][6]= '0';
//                            } else if (i == 4 && board[6][6] == '2') {
//                                board[6][6]= '0';
//                            } else if (i == 5 && board[6][3] == '2') {
//                                board[6][3]= '0';
//                            } else if (i == 6 && board[6][0] == '2') {
//                                board[6][0]= '0';
//                            } else if (i == 7 && board[3][0] == '2') {
//                                board[3][0]= '0';
//                            } else if (i == 8 && board[1][1] == '2') {
//                                board[1][1]= '0';
//                            } else if (i == 9 && board[1][3] == '2') {
//                                board[1][3]= '0';
//                            } else if (i == 10 && board[1][5] == '2') {
//                                board[1][5]= '0';
//                            } else if (i == 11 && board[3][5] == '2') {
//                                board[3][5]= '0';
//                            } else if (i == 12 && board[5][5] == '2') {
//                                board[5][5]= '0';
//                            } else if (i == 13 && board[5][3] == '2') {
//                                board[5][3]= '0';
//                            } else if (i == 14 && board[5][1] == '2') {
//                                board[5][1]= '0';
//                            } else if (i == 15 && board[3][1] == '2') {
//                                board[3][1]= '0';
//                            } else if (i == 16 && board[2][2] == '2') {
//                                board[2][2]= '0';
//                            } else if (i == 17 && board[2][3] == '2') {
//                                board[2][3]= '0';
//                            } else if (i == 18 && board[2][4] == '2') {
//                                board[2][4]= '0';
//                            } else if (i == 19 && board[3][4] == '2') {
//                                board[3][4]= '0';
//                            } else if (i == 20 && board[4][4] == '2') {
//                                board[4][4]= '0';
//                            } else if (i == 21 && board[4][3] == '2') {
//                                board[4][3]= '0';
//                            } else if (i == 22 && board[4][2] == '2') {
//                                board[4][2]= '0';
//                            } else if (i == 23 && board[3][2] == '2') {
//                                board[3][2]= '0';
//                            } 
                        }else if (Turn == 1 && setPiece.get(i).toString().equalsIgnoreCase("1")){
                            setPiece.set(i, 0);
                            mill = false;
                            PlayerOneBoard--;
                            label.setText("Player " + Turn + "'s piece removed");
                            
                            
                            int[] tempI = convertPiecetoNumXY(i);
                            if (board[tempI[0]][tempI[1]] == '1') {
                                board[tempI[0]][tempI[1]] = '0';
                                //JOptionPane.showMessageDialog(null,"work1", "Information", JOptionPane.INFORMATION_MESSAGE);
                            }
                            //IS REPLACED BY ABOUT METHOD
//                            if (i == 0 && board[0][0] == '1') {
//                                board[0][0] = '0';
//                            } else if (i == 1 && board[0][3] == '1') {
//                                board[0][3]= '0';
//                            } else if (i == 2 && board[0][6] == '1') {
//                                board[0][6]= '0';
//                            } else if (i == 3 && board[3][6] == '1') {
//                                board[3][6]= '0';
//                            } else if (i == 4 && board[6][6] == '1') {
//                                board[6][6]= '0';
//                            } else if (i == 5 && board[6][3] == '1') {
//                                board[6][3]= '0';
//                            } else if (i == 6 && board[6][0] == '1') {
//                                board[6][0]= '0';
//                            } else if (i == 7 && board[3][0] == '1') {
//                                board[3][0]= '0';
//                            } else if (i == 8 && board[1][1] == '1') {
//                                board[1][1]= '0';
//                            } else if (i == 9 && board[1][3] == '1') {
//                                board[1][3]= '0';
//                            } else if (i == 10 && board[1][5] == '1') {
//                                board[1][5]= '0';
//                            } else if (i == 11 && board[3][5] == '1') {
//                                board[3][5]= '0';
//                            } else if (i == 12 && board[5][5] == '1') {
//                                board[5][5]= '0';
//                            } else if (i == 13 && board[5][3] == '1') {
//                                board[5][3]= '0';
//                            } else if (i == 14 && board[5][1] == '1') {
//                                board[5][1]= '0';
//                            } else if (i == 15 && board[3][1] == '1') {
//                                board[3][1]= '0';
//                            } else if (i == 16 && board[2][2] == '1') {
//                                board[2][2]= '0';
//                            } else if (i == 17 && board[2][3] == '1') {
//                                board[2][3]= '0';
//                            } else if (i == 18 && board[2][4] == '1') {
//                                board[2][4]= '0';
//                            } else if (i == 19 && board[3][4] == '1') {
//                                board[3][4]= '0';
//                            } else if (i == 20 && board[4][4] == '1') {
//                                board[4][4]= '0';
//                            } else if (i == 21 && board[4][3] == '1') {
//                                board[4][3]= '0';
//                            } else if (i == 22 && board[4][2] == '1') {
//                                board[4][2]= '0';
//                            } else if (i == 23 && board[3][2] == '1') {
//                                board[3][2]= '0';
//                            } 
                        }else {
                            label.setText("Wrong Peg, select one of the other player's pegs");
                        } 

                    }
                }

                repaint();

            }

            /*determines if one of the players has less than three pieces, which ends the game */
            if(PlayerOneBoard < 3 && PlayerOneCount == 9){
                label.setText("Player 2 wins");
                gameOver = true;
            }
            else if(PlayerTwoBoard < 3 && PlayerTwoCount == 9){
                label.setText("Player 1 wins");
                gameOver = true;
            }
        }
        
    }
    
    /*
     * This metod convert board x,y coordinate to location number of each piece, can be use several time
     */
        
    public int convertXYtoPieceNum(int x, int y) {
        int result = -1;
        if (x == 0 && y == 0){
            result = 0;
        }else if (x==0 && y ==3 ){
            result = 1;
        }else if (x==0 && y ==6 ){
            result = 2;
        }else if (x==3 && y ==6 ){
            result = 3;
        }else if (x==6 && y ==6 ){
            result = 4;
        }else if (x==6 && y ==3 ){
            result = 5;
        }else if (x==6 && y ==0 ){
            result = 6;
        }else if (x==3 && y ==0 ){
            result = 7;
        }else if (x==1 && y ==1 ){
            result = 8;
        }else if (x==1 && y ==3 ){
            result = 9;
        }else if (x==1 && y ==5 ){
            result = 10;
        }else if (x==3 && y ==5 ){
            result = 11;
        }else if (x==5 && y ==5 ){
            result = 12;
        }else if (x==5 && y ==3 ){
            result = 13;
        }else if (x==5 && y ==1 ){
            result = 14;
        }else if (x==3 && y ==1 ){
            result = 15;
        }else if (x==2 && y ==2 ){
            result = 16;
        }else if (x==2 && y ==3 ){
            result = 17;
        }else if (x==2 && y ==4 ){
            result = 18;
        }else if (x==3 && y ==4 ){
            result = 19;
        }else if (x==4 && y ==4 ){
            result = 20;
        }else if (x==4 && y ==3 ){
            result = 21;
        }else if (x==4 && y ==2 ){
            result = 22;
        }else if (x==3 && y ==2 ){
            result = 23;
        }
        
        return result;
    }
    /*
     * This metod convert location number of each pieceboard back to x,y coordinate, can be use several time
     */
    public int[] convertPiecetoNumXY(int i){
        int[] result = {-1,-1};
        if (i == 0 ) {
            result[0]= 0; result[1]=0;
        } else if (i == 1 ) {
            result[0]= 0; result[1]=3;
        } else if (i == 2 ) {
            result[0]= 0; result[1]=6;
        } else if (i == 3 ) {
            result[0]= 3; result[1]=6;
        } else if (i == 4 ) {
            result[0]= 6; result[1]=6;
        } else if (i == 5 ) {
            result[0]= 6; result[1]=3;
        } else if (i == 6 ) {
            result[0]= 6; result[1]=0;
        } else if (i == 7 ) {
            result[0]= 3; result[1]=0;
        } else if (i == 8 ) {
            result[0]= 1; result[1]=1;
        } else if (i == 9 ) {
            result[0]= 1; result[1]=3;
        } else if (i == 10 ) {
            result[0]= 1; result[1]=5;
        } else if (i == 11 ) {
            result[0]= 3; result[1]=5;
        } else if (i == 12 ) {
            result[0]= 5; result[1]=5;
        } else if (i == 13 ) {
            result[0]= 5; result[1]=3;
        } else if (i == 14 ) {
            result[0]= 5; result[1]=1;
        } else if (i == 15 ) {
            result[0]= 3; result[1]=1;
        } else if (i == 16 ) {
            result[0]= 2; result[1]=2;
        } else if (i == 17 ) {
            result[0]= 2; result[1]=3;
        } else if (i == 18 ) {
            result[0]= 2; result[1]=4;
        } else if (i == 19 ) {
            result[0]= 3; result[1]=4;
        } else if (i == 20 ) {
            result[0]= 4; result[1]=4;
        } else if (i == 21 ) {
            result[0]= 4; result[1]=3;
        } else if (i == 22 ) {
            result[0]= 4; result[1]=2;
        } else if (i == 23 ) {
            result[0]= 3; result[1]=2;
        }
         return result;
    }
    public void mouseExited(MouseEvent e) {
    }

    public void mousePressed(MouseEvent e) {
    }

    public void mouseReleased(MouseEvent e) {
    }

    public void mouseEntered(MouseEvent e) {
    }
}
